package myweb.myUdp;

import java.io.IOException;
import java.net.*;

public class UDPSend {

    public static void main(String[] args) {
        try {
            DatagramSocket ds = new DatagramSocket();
            String str = "hello word";
            DatagramPacket dp = new DatagramPacket(str.getBytes(), str.length(),
                    InetAddress.getByName("127.0.0.1"),3000);
            System.out.println("udpSend : i will send a message");
            ds.send(dp);
            System.out.println("udpSend: Had a message");
            Thread.sleep(1000);
            byte[] buf = new byte[1024];
            DatagramPacket dp2 = new DatagramPacket(buf, 1024);
            System.out.println("UdpSend:wait  a message ing");
            ds.receive(dp2);
            System.out.println("udpSend:received one message ");
            String receivedStr = new String(dp2.getData(),0,dp2.getLength()) +
            "from"+dp2.getAddress().getHostAddress()+":"+dp2.getPort();
            System.out.println(receivedStr);
            ds.close();
        } catch (SocketException | UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
